/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bulkmailer.email.sender;

import bulkmailer.beans.AccountSettings;
import bulkmailer.beans.ServerSettings;
import bulkmailer.app.main.UI;
import com.ivasoft.libemailj.Email;
import com.ivasoft.libemailj.EmailDispatcher;
import com.ivasoft.libemailj.EmailDispatcherException;
import java.util.Iterator;
import java.util.TreeSet;
import org.apache.log4j.Logger;

/**
 *
 * @author ravi
 */
public class SenderThread extends Thread {

    private ServerSettings serverSettings = null;
    private AccountSettings accountSettings = null;
    private Email email = null;
    private bulkmailer.beans.Email mailBean = null;
    private TreeSet<String> addrSet = null;

    public SenderThread(ServerSettings serverSettings, AccountSettings accountSettings, bulkmailer.beans.Email mail, TreeSet<String> addrSet) {
        this.serverSettings = serverSettings;
        this.accountSettings = accountSettings;
        this.addrSet = addrSet;
        this.mailBean = mail;
    }

    private boolean isValid(String email) {
        String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
        String email1 = email;
        Boolean b = email1.matches(EMAIL_REGEX);
        return b;
    }

    @Override
    public void run() {
        try {
            EmailDispatcher emailDispather = null;
            emailDispather = new EmailDispatcher();
            Iterator iter = addrSet.iterator();
            String addr = null;
            while (iter.hasNext()) {
                emailDispather.connect(serverSettings.getSmtpAddress(), serverSettings.getSmtpPort(), accountSettings.getEmailId(), accountSettings.getPassword());
                email = new Email(emailDispather.getSession(), Email.MessageType.MIME_MESSAGE);
                addr = iter.next().toString();
                if (addr == null && addr.equals(" ")) {
                    Logger.getRootLogger().debug("Invalid email id:" + addr);
                    continue;
                } else {
                    if (isValid(addr) == true) {
                        Logger.getRootLogger().debug("Valid email id:" + addr);
                        email.setToList(addr);
                    }
                }
                this.email.setContent(mailBean.getBody() + "<br><br>" + mailBean.getSign(), Email.MessageFormat.HTML);
                this.email.setSubject(mailBean.getSubject());
                Logger.getRootLogger().debug("email id: " + accountSettings.getEmailId());
                Logger.getRootLogger().debug("sender name: " + accountSettings.getSenderName());
                email.setSender(accountSettings.getSenderName(), accountSettings.getEmailId());
                email.sentDate(new java.util.Date());
                Logger.getRootLogger().debug("sending to: " + addr);
                emailDispather.sendEmail(email);
                Logger.getRootLogger().debug("sending complete");
                UI.appsUi.markMailSentProgress();
                emailDispather.close();
            }
        } catch (EmailDispatcherException ex) {
            Logger.getRootLogger().error(ex.toString());
        } catch (Exception ex) {
            Logger.getRootLogger().error(ex.toString());
        }
    }
}
